package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.comment.domain.QuestionAnswer;
import cn.wolfcode.luowowo.comment.query.QuestionAnswerQuery;
import cn.wolfcode.luowowo.comment.service.IQuestionAnswerService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.community.domain.Question;
import cn.wolfcode.luowowo.community.domain.QuestionContent;
import cn.wolfcode.luowowo.community.service.IQuestionContentService;
import cn.wolfcode.luowowo.community.service.IQuestionService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.service.IQuestionAnswerTemplateService;
import cn.wolfcode.luowowo.search.vo.QAStatisVO;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("community")
public class CommunityController {

    @Reference
    private IQuestionService questionService;
    @Reference
    private IQuestionContentService questionContentService;
    @Reference
    private IQuestionAnswerService questionAnswerService;
    @Reference
    private IQuestionAnswerTemplateService questionAnswerTemplateService;

    //提问页面跳转
    @RequestMapping("/public")
    public String question(){

        return "community/public";
    }

    //问题发布
    @RequestMapping("/publish")
    @ResponseBody
    public Object publish(Question question,String content,@UserParam UserInfo userInfo){

        question.setUser_id(userInfo.getId());
        question.setUser_name(userInfo.getNickname());
        QuestionContent questionContent = new QuestionContent();
        Long questionId = questionService.save(question);

        questionContent.setId(questionId);
        questionContent.setContent(content);
        questionContentService.save(questionContent);

        return new AjaxResult().addData(questionId);
    }

    //问答内容和评论显示
    @RequestMapping("/detail")
    public String detail(Model model,@ModelAttribute("qo") QuestionAnswerQuery qo,@UserParam UserInfo userInfo){
        Question question  = questionService.getQuestionDetailById(qo.getQuestionId());
        String content = questionContentService.getQuestionContentById(qo.getQuestionId());

        Page page = questionAnswerService.query(qo);

        List<QuestionAnswer> contents = page.getContent();
        for (QuestionAnswer qa : contents) {
            System.out.println(qa.toString());
        }
        model.addAttribute("question",question);
        model.addAttribute("content",content);
        model.addAttribute("page",page);
        model.addAttribute("currentUser",userInfo);


        return "community/wendaDetail";
    }

    //提交回答
    @RequestMapping("/submitAnswer")
    @ResponseBody
    public Object submitAnswer(QuestionAnswer answer, @UserParam UserInfo userInfo,Long questionUserId){
//        if(userInfo.getId() == questionUserId){
//            return AjaxResult.FAIL;
//        }
        System.out.println(answer.toString());
        answer.setHeadUrl(userInfo.getHeadImgUrl());
        answer.setLevel(userInfo.getLevel());
        answer.setUsername(userInfo.getNickname());
        answer.setUserId(userInfo.getId());
        questionAnswerService.save(answer);
        return AjaxResult.SUCCESS;
    }

    //社区问答首页
    @RequestMapping("")
    public String index(Model model,QuestionAnswerQuery qo){
        //获取所有的问题ID
        List<Long> qIds = questionService.getAllQuestionId();

        Page page = questionAnswerService.queryForList(qo,qIds);

//        List<QuestionAnswer> content = page.getContent();
//        for (QuestionAnswer questionAnswer : content) {
//            System.out.println(questionAnswer);
//        }
        model.addAttribute("page",page);

        //问答排行榜
        List<QAStatisVO> qaRank= questionAnswerTemplateService.queryQARank();
        for (QAStatisVO qaStatisVO : qaRank) {
            System.out.println(qaStatisVO);
        }

        QAStatisVO first = qaRank.get(0);
        /*QAStatisVO second = qaRank.get(1);
        QAStatisVO third = qaRank.get(2);*/
//        List<QAStatisVO> secend = qaRank.subList(1, 2);
//        List<QAStatisVO> third = qaRank.subList(2, 3);

        model.addAttribute("first",first);
        /*model.addAttribute("second",second);
        model.addAttribute("third",third);*/
//        model.addAttribute("secend",secend);
//        model.addAttribute("third",third);
        return "community/wenda";
    }




}
